<?php
require_once('pownceoauth.class.php');

class pownceApi extends pownceAuth 
{
  
  protected $error;
  
  protected $internel_error;
  public function __construct($type, $key, $secret, $username = null, $password = null)
  {
    switch($type)
    {
      case 'oauth':
        $this->initOauth();
        $this->buildConsumer($key, $secret);
        break;
      default:
        $this->initBasic();
        break;
    }
  }
  
  public function getNoteList($username, $limit)
  {
    $data = array(
      'limit'    => $limit,
    );
    $this->setPownceOptions($this->build_postfields_submit($data), true);
    $this->setPownceType('GET');
    $this->setPownceApiCall(OAUTH_NOTELIST_URL . $username.'.xml');
    $this->setOauthRequest(OAuthRequest::from_consumer_and_token($this->getOauthConsumer(), $this->getOauthAccessToken(), $this->getPownceType(), $this->getPownceApiCall()));
    $this->setParameters($data, $this->getOauthRequest());
    $this->getOauthRequest()->sign_request($this->getOauthSignatureMethod(), $this->getOauthConsumer(), $this->getOauthAccessToken());
    $this->setPownceHeaders($this->getOauthRequest()->to_header());
    $this->execCurl();
    $this->validatePownceResponse();
    if(!$this->getError())
    {
      return $this->getPownceResponse();
    }
  }
  
  public function sendMessage($to, $body)
  {
    $data = array(
      'note_to'    => $to,
      'note_body'  => $body
    );
    $this->setPownceOptions($this->build_postfields_submit($data), true);
    $this->setPownceType('POST');
    $this->setPownceApiCall(OAUTH_SENDMESSAGE_URL);
    $this->setOauthRequest(OAuthRequest::from_consumer_and_token($this->getOauthConsumer(), $this->getOauthAccessToken(), $this->getPownceType(), $this->getPownceApiCall()));
    $this->setParameters($data, $this->getOauthRequest());
    $this->getOauthRequest()->sign_request($this->getOauthSignatureMethod(), $this->getOauthConsumer(), $this->getOauthAccessToken());
    $this->setPownceHeaders($this->getOauthRequest()->to_header());
    $this->execCurl();
    $this->validatePownceResponse();
    if(!$this->getError())
    {
      return $this->getPownceResponse();
    }
  }  
  public function sendLink($url, $to, $body = null)
  {
    $data = array(
      'note_to'    => $to,
      'url'        => $url
    );
    if($body)
    {
      $data['note_body'] = $body;
    }
    $this->setPownceOptions($this->build_postfields_submit($data), true);
    $this->setPownceType('POST');
    $this->setPownceApiCall(OAUTH_SENDLINK_URL);
    $this->setOauthRequest(OAuthRequest::from_consumer_and_token($this->getOauthConsumer(), $this->getOauthAccessToken(), $this->getPownceType(), $this->getPownceApiCall()));
    $this->setParameters($data, $this->getOauthRequest());
    $this->getOauthRequest()->sign_request($this->getOauthSignatureMethod(), $this->getOauthConsumer(), $this->getOauthAccessToken());
    $this->setPownceHeaders($this->getOauthRequest()->to_header());
    $this->execCurl();
    $this->validatePownceResponse();
    if(!$this->getError())
    {
      return $this->getPownceResponse();
    }
  }
  
  public function sendEvent($to, $event_name, $event_location, $event_date, $body = null)
  {
    //TODO: Check whether date is in past.
    $data = array(
      'note_to'           => $to,
      'event_name'        => $event_name,
      'event_location'    => $event_location,
      'event_date'        => $event_date
    );
    if($body)
    {
      $data['note_body'] = $body;
    }
    $this->setPownceOptions($this->build_postfields_submit($data), true);
    $this->setPownceType('POST');
    $this->setPownceApiCall(OAUTH_SENDEVENT_URL);
    $this->setOauthRequest(OAuthRequest::from_consumer_and_token($this->getOauthConsumer(), $this->getOauthAccessToken(), $this->getPownceType(), $this->getPownceApiCall()));
    $this->setParameters($data, $this->getOauthRequest());
    $this->getOauthRequest()->sign_request($this->getOauthSignatureMethod(), $this->getOauthConsumer(), $this->getOauthAccessToken());
    $this->setPownceHeaders($this->getOauthRequest()->to_header());
    $this->execCurl();
    $this->validatePownceResponse();
    if(!$this->getError())
    {
      return $this->getPownceResponse();
    }
  }
  public function sendFile($to, $media_file, $body = null)
  {
    //TODO: Check whether date is in past.
    $data = array(
      'note_to'           => $to,
      'media_file'        => '@' .$media_file
    );
    if($body)
    {
      $data['note_body'] = $body;
    }
    $this->setPownceOptions($this->build_postfields_submit($data), true);
    $this->setPownceType('POST');
    $this->setPownceApiCall(OAUTH_SENDFILE_URL);
    $this->setOauthRequest(OAuthRequest::from_consumer_and_token($this->getOauthConsumer(), $this->getOauthAccessToken(), $this->getPownceType(), $this->getPownceApiCall()));
    $this->setParameters($data, $this->getOauthRequest());
    $this->getOauthRequest()->sign_request($this->getOauthSignatureMethod(), $this->getOauthConsumer(), $this->getOauthAccessToken());
    $this->setPownceHeaders($this->getOauthRequest()->to_header());
    $this->execCurl();
    $this->validatePownceResponse();
    if(!$this->getError())
    {
      return $this->getPownceResponse();
    }
  }
  
  public function getSendToList()
  {
    $this->setPownceType('GET');
    $this->setPownceApiCall(OAUTH_SENDLIST_URL);
    $this->setOauthRequest(OAuthRequest::from_consumer_and_token($this->getOauthConsumer(), $this->getOauthAccessToken(), $this->getPownceType(), $this->getPownceApiCall()));
    $this->getOauthRequest()->sign_request($this->getOauthSignatureMethod(), $this->getOauthConsumer(), $this->getOauthAccessToken());
    $this->setParameters($data, $this->getOauthRequest());
    $this->setPownceHeaders($this->getOauthRequest()->to_header());
    $this->execCurl();
    $this->validatePownceResponse();
    if(!$this->getError())
    {
      return $this->getPownceResponse();
    }
  }
  
  public function validatePownceResponse()
  {
    if($this->getPownceResponse()){
      $xml = new SimpleXMLElement($this->getPownceResponse());
      switch($xml->status_code)
      {
        case '404':
          $this->setError($xml->message);
          break;
        case '401':
          $this->setError($xml->message);
          break;
        default:
          $this->setError(null);
      }
    }
    else {
      print 'Having trouble communication with Pownce. Try again later.';
    }
 
  }
 /**
 * Builds out our post fields and ass theme to curl so we can post to pownce api
 *
 * @param array $data
 * @param curl object $chandle
 * @return curl object
 */
  private function build_postfields_submit($data = array()) {
    $post_data = '';  
    if (is_array($data) && !empty($data)) {
      array_walk($data, array($this, 'flatten_fields'));
      $post_data = implode('&', $data);
    }
    return $post_data;
  }
  
  private function setParameters($array, $request) {
    if(is_array($array) && !empty($array)) {
      foreach($array as $key=>$value) {
        $request->set_parameter($key, str_replace($value,'%252B'));
      } 
    }
  }
/**
 * Modifies our array data so we can turn it into a querystring
 * 
 * @param string $item - array value
 * @param string $key  - key of the array element
 */
  private function flatten_fields(&$item, $key) {
    $item = $key .'='. $item;
  }
  
  public function getError()
  {
    return $this->error;
  }
  
  public function setError($value)
  {
    $this->error = $value;
  }
  
  public function getInternelError()
  {
    return $this->internel_error;
  }
  
  public function setInternelError($value) 
  {
    $this->internel_error = $value;
  }
}

?>